layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

	let year = new Date().getFullYear();//年
	let month = new Date().getMonth() + 1;//注意！月份是从0月开始获取的，所以要+1;

    laydate.render({
        elem: '#startTime',
 		format: 'yyyy-MM',
		type:'month',
		value:year + '-' + (month >= 10 ? month : '0' + month)
    });

    /**
     * 调度单表管理
     */
    var TDispatch = {
        tableId: "tDispatchTable"
    };

	var _data = []
	var sy = ""
	var by = ""
	reds(year + '-' + (month >= 10 ? month : '0' + month));
	
	function reds(month){
		$.ajax({
			type: 'get',
			data: {month:month},
			url: "/panel/w6tms",
			async: false,
			dataType:"json",
			success: function (data) {
				if(data.data.length == 0){
    				yer.msg('未查询到数据'); 
					return
				}
				_data = []
				sy = ""
				by = ""
				console.log($.parseJSON(data.data).result)
				let arr = $.parseJSON(data.data).result
				var obj1 = {}
				obj1.project = '九州通配送'
				obj1.qssx = '0'
				obj1.qsl = '0%'
				obj1.sql = '0%'
				
				obj1.h12 = '0%'
				obj1.h24 = '0%'
				obj1.h48 = '0%'
				
				obj1.mzl = '0%'
				
				obj1.sy = '0'
				obj1.by = '0'
				
				var obj2 = {}
				obj2.project = '中顺洁柔孝感外租仓'
				obj2.qssx = '0'
				obj2.qsl = '0%'
				obj2.sql = '0%'
				
				obj2.h12 = '0%'
				obj2.h24 = '0%'
				obj2.h48 = '0%'
				
				obj2.mzl = '0%'
				
				obj2.sy = '0'
				obj2.by = '0'
				
				var obj3 = {}
				obj3.project = '银座配送'
				obj3.qssx = '0'
				obj3.qsl = '0%'
				obj3.sql = '0%'
				
				obj3.h12 = '0%'
				obj3.h24 = '0%'
				obj3.h48 = '0%'
				
				obj3.mzl = '0%'
				
				obj3.sy = '0'
				obj3.by = '0'
				
				var obj4 = {}
				obj4.project = '山东正商共配'
				obj4.qssx = '0'
				obj4.qsl = '0%'
				obj4.sql = '0%'
				
				obj4.h12 = '0%'
				obj4.h24 = '0%'
				obj4.h48 = '0%'
				
				obj4.mzl = '0%'
				
				obj4.sy = '0'
				obj4.by = '0'
				
				var obj5 = {}
				obj5.project = '成都'
				obj5.qssx = '0'
				obj5.qsl = '0%'
				obj5.sql = '0%'
				
				obj5.h12 = '0%'
				obj5.h24 = '0%'
				obj5.h48 = '0%'
				
				obj5.mzl = '0%'
				
				obj5.sy = '0'
				obj5.by = '0'
				
				var obj6 = {}
				obj6.project = '重庆'
				obj6.qssx = '0'
				obj6.qsl = '0%'
				obj6.sql = '0%'
				
				obj6.h12 = '0%'
				obj6.h24 = '0%'
				obj6.h48 = '0%'
				
				obj6.mzl = '0%'
				
				obj6.sy = '0'
				obj6.by = '0'
				
				var obj7 = {}
				obj7.project = '上虞协同仓'
				obj7.qssx = '0'
				obj7.qsl = '0%'
				obj7.sql = '0%'
				
				obj7.h12 = '0%'
				obj7.h24 = '0%'
				obj7.h48 = '0%'
				
				obj7.mzl = '0%'
				
				obj7.sy = '0'
				obj7.by = '0'
				
				var obj8 = {}
				obj8.project = '江西'
				obj8.qssx = '0'
				obj8.qsl = '0%'
				obj8.sql = '0%'
				
				obj8.h12 = '0%'
				obj8.h24 = '0%'
				obj8.h48 = '0%'
				
				obj8.mzl = '0%'
				
				obj8.sy = '0'
				obj8.by = '0'
				
				var obj9 = {}
				obj9.project = '哈尔滨'
				obj9.qssx = '0'
				obj9.qsl = '0%'
				obj9.sql = '0%'
				
				obj9.h12 = '0%'
				obj9.h24 = '0%'
				obj9.h48 = '0%'
				
				obj9.mzl = '0%'
				
				obj9.sy = '0'
				obj9.by = '0'
				
				var obj10 = {}
				obj10.project = '（济南）'
				obj10.qssx = '0'
				obj10.qsl = '0%'
				obj10.sql = '0%'
				
				obj10.h12 = '0%'
				obj10.h24 = '0%'
				obj10.h48 = '0%'
				
				obj10.mzl = '0%'
				
				obj10.sy = '0'
				obj10.by = '0'
				
				var obj11 = {}
				obj11.project = '贝因美'
				obj11.qssx = '0'
				obj11.qsl = '0%'
				obj11.sql = '0%'
				
				obj11.h12 = '0%'
				obj11.h24 = '0%'
				obj11.h48 = '0%'
				
				obj11.mzl = '0%'
				
				obj11.sy = '0'
				obj11.by = '0'
				
				var obj12 = {}
				obj12.project = '华熙TOB仓'
				obj12.qssx = '0'
				obj12.qsl = '0%'
				obj12.sql = '0%'
				
				obj12.h12 = '0%'
				obj12.h24 = '0%'
				obj12.h48 = '0%'
				
				obj12.mzl = '0%'
				
				obj12.sy = '0'
				obj12.by = '0'
				
				var obj13 = {}
				obj13.project = '宏济堂'
				obj13.qssx = '0'
				obj13.qsl = '0%'
				obj13.sql = '0%'
				
				obj13.h12 = '0%'
				obj13.h24 = '0%'
				obj13.h48 = '0%'
				
				obj13.mzl = '0%'
				
				obj13.sy = '0'
				obj13.by = '0'
				
				var obj14 = {}
				obj14.project = '冷链云仓'
				obj14.qssx = '0'
				obj14.qsl = '0%'
				obj14.sql = '0%'
				
				obj14.h12 = '0%'
				obj14.h24 = '0%'
				obj14.h48 = '0%'
				
				obj14.mzl = '0%'
				
				obj14.sy = '0'
				obj14.by = '0'
				
				var obj15 = {}
				obj15.project = '高新人力'
				obj15.qssx = '0'
				obj15.qsl = '0%'
				obj15.sql = '0%'
				
				obj15.h12 = '0%'
				obj15.h24 = '0%'
				obj15.h48 = '0%'
				
				obj15.mzl = '0%'
				
				obj15.sy = '0'
				obj15.by = '0'
				
				var obj16 = {}
				obj16.project = '兰州'
				obj16.qssx = '0'
				obj16.qsl = '0%'
				obj16.sql = '0%'
				
				obj16.h12 = '0%'
				obj16.h24 = '0%'
				obj16.h48 = '0%'
				
				obj16.mzl = '0%'
				
				obj16.sy = '0'
				obj16.by = '0'
				
				var obj17 = {}
				obj17.project = '南京'
				obj17.qssx = '0'
				obj17.qsl = '0%'
				obj17.sql = '0%'
				
				obj17.h12 = '0%'
				obj17.h24 = '0%'
				obj17.h48 = '0%'
				
				obj17.mzl = '0%'
				
				obj17.sy = '0'
				obj17.by = '0'
				
				var obj18 = {}
				obj18.project = '青岛'
				obj18.qssx = '0'
				obj18.qsl = '0%'
				obj18.sql = '0%'
				
				obj18.h12 = '0%'
				obj18.h24 = '0%'
				obj18.h48 = '0%'
				
				obj18.mzl = '0%'
				
				obj18.sy = '0'
				obj18.by = '0'
				
				var obj19 = {}
				obj19.project = '济南软风'
				obj19.qssx = '0'
				obj19.qsl = '0%'
				obj19.sql = '0%'
				
				obj19.h12 = '0%'
				obj19.h24 = '0%'
				obj19.h48 = '0%'
				
				obj19.mzl = '0%'
				
				obj19.sy = '0'
				obj19.by = '0'
				
				var obj20 = {}
				obj20.project = '石家庄'
				obj20.qssx = '0'
				obj20.qsl = '0%'
				obj20.sql = '0%'
				
				obj20.h12 = '0%'
				obj20.h24 = '0%'
				obj20.h48 = '0%'
				
				obj20.mzl = '0%'
				
				obj20.sy = '0'
				obj20.by = '0'
				
				var obj21 = {}
				obj21.project = '汕头'
				obj21.qssx = '0'
				obj21.qsl = '0%'
				obj21.sql = '0%'
				
				obj21.h12 = '0%'
				obj21.h24 = '0%'
				obj21.h48 = '0%'
				
				obj21.mzl = '0%'
				
				obj21.sy = '0'
				obj21.by = '0'
				
				var obj22 = {}
				obj22.project = '上虞电商'
				obj22.qssx = '0'
				obj22.qsl = '0%'
				obj22.sql = '0%'
				
				obj22.h12 = '0%'
				obj22.h24 = '0%'
				obj22.h48 = '0%'
				
				obj22.mzl = '0%'
				
				obj22.sy = '0'
				obj22.by = '0'
				
				var obj23 = {}
				obj23.project = '苏州'
				obj23.qssx = '0'
				obj23.qsl = '0%'
				obj23.sql = '0%'
				
				obj23.h12 = '0%'
				obj23.h24 = '0%'
				obj23.h48 = '0%'
				
				obj23.mzl = '0%'
				
				obj23.sy = '0'
				obj23.by = '0'
				
				var obj24 = {}
				obj24.project = '天津'
				obj24.qssx = '0'
				obj24.qsl = '0%'
				obj24.sql = '0%'
				
				obj24.h12 = '0%'
				obj24.h24 = '0%'
				obj24.h48 = '0%'
				
				obj24.mzl = '0%'
				
				obj24.sy = '0'
				obj24.by = '0'
				
				var obj25 = {}
				obj25.project = '潍坊（电商）'
				obj25.qssx = '0'
				obj25.qsl = '0%'
				obj25.sql = '0%'
				
				obj25.h12 = '0%'
				obj25.h24 = '0%'
				obj25.h48 = '0%'
				
				obj25.mzl = '0%'
				
				obj25.sy = '0'
				obj25.by = '0'
				
				var obj26 = {}
				obj26.project = '潍坊商贸'
				obj26.qssx = '0'
				obj26.qsl = '0%'
				obj26.sql = '0%'
				
				obj26.h12 = '0%'
				obj26.h24 = '0%'
				obj26.h48 = '0%'
				
				obj26.mzl = '0%'
				
				obj26.sy = '0'
				obj26.by = '0'
				
				var obj27 = {}
				obj27.project = '潍坊干线'
				obj27.qssx = '0'
				obj27.qsl = '0%'
				obj27.sql = '0%'
				
				obj27.h12 = '0%'
				obj27.h24 = '0%'
				obj27.h48 = '0%'
				
				obj27.mzl = '0%'
				
				obj27.sy = '0'
				obj27.by = '0'
				
				var obj28 = {}
				obj28.project = '芜湖（商贸）'
				obj28.qssx = '0'
				obj28.qsl = '0%'
				obj28.sql = '0%'
				
				obj28.h12 = '0%'
				obj28.h24 = '0%'
				obj28.h48 = '0%'
				
				obj28.mzl = '0%'
				
				obj28.sy = '0'
				obj28.by = '0'
				
				var obj29 = {}
				obj29.project = '芜湖大润发'
				obj29.qssx = '0'
				obj29.qsl = '0%'
				obj29.sql = '0%'
				
				obj29.h12 = '0%'
				obj29.h24 = '0%'
				obj29.h48 = '0%'
				
				obj29.mzl = '0%'
				
				obj29.sy = '0'
				obj29.by = '0'
				
				var obj30 = {}
				obj30.project = '芜湖干线'
				obj30.qssx = '0'
				obj30.qsl = '0%'
				obj30.sql = '0%'
				
				obj30.h12 = '0%'
				obj30.h24 = '0%'
				obj30.h48 = '0%'
				
				obj30.mzl = '0%'
				
				obj30.sy = '0'
				obj30.by = '0'
				
				var obj31 = {}
				obj31.project = '温州'
				obj31.qssx = '0'
				obj31.qsl = '0%'
				obj31.sql = '0%'
				
				obj31.h12 = '0%'
				obj31.h24 = '0%'
				obj31.h48 = '0%'
				
				obj31.mzl = '0%'
				
				obj31.sy = '0'
				obj31.by = '0'
				
				var obj32 = {}
				obj32.project = '孝感运营中心'
				obj32.qssx = '0'
				obj32.qsl = '0%'
				obj32.sql = '0%'
				
				obj32.h12 = '0%'
				obj32.h24 = '0%'
				obj32.h48 = '0%'
				
				obj32.mzl = '0%'
				
				obj32.sy = '0'
				obj32.by = '0'
				
				var obj33 = {}
				obj33.project = '泉林本色'
				obj33.qssx = '0'
				obj33.qsl = '0%'
				obj33.sql = '0%'
				
				obj33.h12 = '0%'
				obj33.h24 = '0%'
				obj33.h48 = '0%'
				
				obj33.mzl = '0%'
				
				obj33.sy = '0'
				obj33.by = '0'
				
				var obj34 = {}
				obj34.project = '历城云仓'
				obj34.qssx = '0'
				obj34.qsl = '0%'
				obj34.sql = '0%'
				
				obj34.h12 = '0%'
				obj34.h24 = '0%'
				obj34.h48 = '0%'
				
				obj34.mzl = '0%'
				
				obj34.sy = '0'
				obj34.by = '0'
				
				var obj35 = {}
				obj35.project = '园区'
				obj35.qssx = '0'
				obj35.qsl = '0%'
				obj35.sql = '0%'
				
				obj35.h12 = '0%'
				obj35.h24 = '0%'
				obj35.h48 = '0%'
				
				obj35.mzl = '0%'
				
				obj35.sy = '0'
				obj35.by = '0'
				
				var obj36 = {}
				obj36.project = '总部运力调度'
				obj36.qssx = '0'
				obj36.qsl = '0%'
				obj36.sql = '0%'
				
				obj36.h12 = '0%'
				obj36.h24 = '0%'
				obj36.h48 = '0%'
				
				obj36.mzl = '0%'
				
				obj36.sy = '0'
				obj36.by = '0'
				
				var obj37 = {}
				obj37.project = '浙北商贸'
				obj37.qssx = '0'
				obj37.qsl = '0%'
				obj37.sql = '0%'
				
				obj37.h12 = '0%'
				obj37.h24 = '0%'
				obj37.h48 = '0%'
				
				obj37.mzl = '0%'
				
				obj37.sy = '0'
				obj37.by = '0'
				
				_data.push(obj1)
				_data.push(obj2)
				_data.push(obj3)
				_data.push(obj4)
				_data.push(obj5)
				_data.push(obj6)
				_data.push(obj7)
				_data.push(obj8)
				_data.push(obj9)
				_data.push(obj10)
				_data.push(obj11)
				_data.push(obj12)
				_data.push(obj13)
				_data.push(obj14)
				_data.push(obj15)
				_data.push(obj16)
				_data.push(obj17)
				_data.push(obj18)
				_data.push(obj19)
				_data.push(obj20)
				_data.push(obj21)
				_data.push(obj22)
				_data.push(obj23)
				_data.push(obj24)
				_data.push(obj25)
				_data.push(obj26)
				_data.push(obj27)
				_data.push(obj28)
				_data.push(obj29)
				_data.push(obj30)
				_data.push(obj31)
				_data.push(obj32)
				_data.push(obj33)
				_data.push(obj34)
				_data.push(obj35)
				_data.push(obj36)
				_data.push(obj37)
				
				for (var i = 0; i < arr[1].DDQSQKFX.length ; i++) {
					for (var j = 0; j < _data.length ; j++) {
						if(arr[1].DDQSQKFX[i].WDMC == _data[j].project){
							_data[j].qssx = arr[1].DDQSQKFX[i].DDQSSX
							_data[j].qsl = arr[1].DDQSQKFX[i].DDQSL+'%'
							_data[j].sql = arr[1].DDQSQKFX[i].HDSCL+'%'
							continue
						}
					}
				}
				
				for (var i = 0; i < arr[2].YDQSSXZB.length ; i++) {
					for (var j = 0; j < _data.length ; j++) {
						if(arr[2].YDQSSXZB[i].WDMC == _data[j].project){
							_data[j].h12 = arr[2].YDQSSXZB[i].QS12ZB+'%'
							_data[j].h24 = arr[2].YDQSSXZB[i].QS24ZB+'%'
							_data[j].h48 = arr[2].YDQSSXZB[i].QS48ZB+'%'
							continue
						}
					}
				}
				
				for (var i = 0; i < arr[3].YDMZLPH.length ; i++) {
					for (var j = 0; j < _data.length ; j++) {
						if(arr[3].YDMZLPH[i].WDMC == _data[j].project){
							_data[j].mzl = arr[3].YDMZLPH[i].MZL+'%'
							continue
						}
					}
				}
				
				for (var i = 0; i < arr[4].YDZFYFSFX.length ; i++) {
					for (var j = 0; j < _data.length ; j++) {
						if(arr[4].YDZFYFSFX[i].WDMC == _data[j].project){
							_data[j].sy = (arr[4].YDZFYFSFX[i].SYYL*1).toFixed(2)
							_data[j].by = (arr[4].YDZFYFSFX[i].BYYL*1).toFixed(2)
							if(sy == ""){
								sy = arr[4].YDZFYFSFX[0].SYBQ+'月'
								by = arr[4].YDZFYFSFX[0].BYBQ+'月'
							}
							continue
						}
					}
				}
			}
		})
    
	    /**
	     * 初始化表格的列
	     */
	    TDispatch.initColumn = function () {
	        return [[
	            {type: 'checkbox',rowspan:2},
	            {field: 'project', title: '项目部' ,rowspan:2, align: "center", totalRowText: '合计：'},
	            {field: '', title: '月度发运方数对比',colspan:2, align: "center"},
	            {field: 'mzl', title: '月度满载率',rowspan:2, align: "center", sort: true},
	            {field: '', title: '月度签收情况',colspan:3, align: "center"},
	            {field: '', title: '月度签收时效占比',colspan:3, align: "center"}
	        ],[
	            {field: 'sy', title: sy, align: "center", sort: true, totalRow: true},
	            {field: 'by', title: by, align: "center", sort: true, totalRow: true},
	            {field: 'qssx', title: '签收时效', align: "center", sort: true},
	            {field: 'qsl', title: '签收率', align: "center", sort: true},
	            {field: 'sql', title: '上传率', align: "center", sort: true},
	            {field: 'h12', title: '12H', align: "center", sort: true},
	            {field: 'h24', title: '24H', align: "center", sort: true},
	            {field: 'h48', title: '48H', align: "center", sort: true}
	        ]];
	    };

	    // 渲染表格
	    var tableResult = table.render({
	        elem: '#' + TDispatch.tableId,
	        data:_data,
	        page: false,
	        limit: 1000,
	        height: "full-60",
	        totalRow: true,
	        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
	        defaultToolbar: ['filter', 'exports', 'print'],
	        cellMinWidth: 100,
	        cols: TDispatch.initColumn()
	    });
	}

    /**
     * 导出excel按钮
     */
    TDispatch.exportExcel = function () {
        var checkRows = table.checkStatus(TDispatch.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 导出excel
    $('#btnExp').click(function () {
        TDispatch.exportExcel();
    });

    /**
     * 点击查询按钮
     */
    TDispatch.search = function () {
		reds($("#startTime").val())
    };

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TDispatch.search();
    });

    // 单击行事件
    table.on('row(' + TDispatch.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + TDispatch.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + TDispatch.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
    });
    
});
